package com.example.lotterysystem.service.activitystatus.operator;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.lotterysystem.common.enums.ActivityPrizeStatusEnum;
import com.example.lotterysystem.dao.dataobject.ActivityPrize;
import com.example.lotterysystem.mapper.ActivityPrizeMapper;
import com.example.lotterysystem.service.dto.TransferActivityStatusDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class PrizeOperator extends AbstractActivityOperator{

    @Autowired
    private ActivityPrizeMapper activityPrizeMapper;

    @Override
    public Integer sequence() {
        return 1;
    }

    @Override
    public Boolean checkTransfer(TransferActivityStatusDTO statusDTO) {
        Long activityId = statusDTO.getActivityId();
        Long prizeId = statusDTO.getPrizeId();
        ActivityPrizeStatusEnum prizeStatus = statusDTO.getTargetPrizeStatus();
        if(prizeStatus == null || prizeId == null || activityId == null){
            return false;
        }
        ActivityPrize activityPrize = activityPrizeMapper.selectOne(
                new QueryWrapper<ActivityPrize>()
                        .eq("activity_id", activityId)
                        .eq("prize_id", prizeId));
        if(activityPrize == null){
            return false;
        }
        if(activityPrize.getStatus().equalsIgnoreCase(prizeStatus.name())){
            return false;
        }
        return true;
    }

    @Override
    public Boolean transfer(TransferActivityStatusDTO statusDTO) {
        try {
            activityPrizeMapper.updateStatus(statusDTO.getActivityId(),
                    statusDTO.getPrizeId(),
                    statusDTO.getTargetPrizeStatus().name());
            return true;
        } catch (Exception e){
            return false;
        }
    }
}
